<?php


namespace app\common\cachemodel\zl1_relation;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class Zl1RelationAnalysisIdeaRecodeInfo extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl1_relation_analysis_idea_recode_info';

    protected $cacheModel = 'relation_analysis.my_idea_recode_info';

    /**
     * 保存 档案数据
     * @param array $data
     */
    public function SaveData(array $data)
    {
        try
        {
            $this->insert($data);
        }
        catch (\Exception $e)
        {
            Log::write('Zl1RelationAnalysisIdeaRecodeInfo model => SaveData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
        }
    }

    /**
     * 更新 档案信息
     * @param int $id
     * @param array $data
     */
    public function UpdateData(int $id, array $data)
    {
        try
        {
            $this->where('id', $id)->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
        }
        catch (\Exception $e)
        {
            Log::write('Zl1RelationAnalysisIdeaRecodeInfo model => UpdateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
        }
    }

    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $field = 'id,self_recode,recode_info';
        try
        {
            $where = ['id' => ['in', $ids]];

            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            if (isset($orderItemData['self_recode']) && $orderItemData['self_recode'])
            {
                $resData[$orderId]['self_recode'] = json_decode($orderItemData['self_recode'], true);
            }

            if (isset($orderItemData['recode_info']) && $orderItemData['recode_info'])
            {
                $resData[$orderId]['recode_info'] = json_decode($orderItemData['recode_info'], true);
            }
        }

        return $resData;
    }
}
